using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MvvmCodeReduction.Model;

namespace MvvmCodeReduction.ViewModel
{
    public class CustomerViewModel : ViewModelBase
    {
        public const string EditText = "Edit";
        public const string CancelText = "Cancel";

        public CustomerViewModel(Customer model)
        {
            Model = model;
        }

        #region Properties

        public Model.Customer Model { get; set; }

        public const string EditedNamePropertyName = "EditedName";
        private string _editedName = string.Empty;
        public string EditedName
        {
            get { return _editedName; }

            set
            {
                if (_editedName == value)
                {
                    return;
                }
                _editedName = value;
                RaisePropertyChanged(EditedNamePropertyName);
            }
        }

        public const string IsEditingPropertyName = "IsEditing";
        private bool _isEditing;
        public bool IsEditing
        {
            get
            {
                return _isEditing;
            }
            set
            {
                if (_isEditing == value)
                {
                    return;
                }
                _isEditing = value;
                RaisePropertyChanged(IsEditingPropertyName);
                RaisePropertyChanged(IsNotEditingPropertyName);
            }
        }

        public const string IsNotEditingPropertyName = "IsNotEditing";
        public bool IsNotEditing
        {
            get { return !_isEditing; }
        }

        public const string EditCommandTextPropertyName = "EditCommandText";
        private string _editCommandText = EditText;
        public string EditCommandText
        {
            get
            {
                return _editCommandText;
            }

            set
            {
                if (_editCommandText == value)
                {
                    return;
                }
                _editCommandText = value;
                RaisePropertyChanged(EditCommandTextPropertyName);
            }
        }

        #endregion Properties

        #region Commands

        private ICommand _saveCommand;
        public ICommand SaveCommand
        {
            get {
                return _saveCommand ??
                       (_saveCommand =
                        new RelayCommand(Save));
            }
        }

        private ICommand _editCommand;
        public ICommand EditCommand
        {
            get
            {
                return _editCommand ??
                       (_editCommand =
                        new RelayCommand(Edit));
            }
        }

        #endregion Commands

        #region Command Handlers

        protected void Save()
        {
            Model.Name = EditedName;
            ToggleEditing();
        }

        protected void Edit()
        {
            ToggleEditing();
        }

        protected virtual void ToggleEditing()
        {
            if (IsEditing)
            {
                EditCommandText = EditText;
                EditedName = string.Empty;
                IsEditing = false;
            }
            else
            {
                EditCommandText = CancelText;
                EditedName = Model.Name;
                IsEditing = true;
            }
        }

        #endregion Command Handlers
    }
}